package com.example.service.impl;

import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mapper.TaskInitMapper;
import com.example.entity.dto.TaskInit;
import com.example.service.TaskInitService;
@Service
public class TaskInitServiceImpl extends ServiceImpl<TaskInitMapper, TaskInit> implements TaskInitService{

	@Override
	public List<TaskInit> selectAllByFormKeyAndTaskId(String formKey,String taskId){
		 return taskInitMapper.selectAllByFormKeyAndTaskId(formKey,taskId);
	}

	@Override
	public List<TaskInit> selectAllByFormKeyAndTaskIds(String formKey, String taskId) {
		return taskInitMapper.selectAllByFormKeyAndTaskIds(formKey,taskId);
	}

	@Override
	public boolean saveOrUpdateTaskInit(TaskInit taskInit) {
		List<TaskInit> taskInits = taskInitMapper.selectAllByFormKeyAndTaskId(taskInit.getFormKey(), taskInit.getTaskId());
		if (taskInits.isEmpty()){
			Boolean b = taskInitMapper.insertWithCadidateUsers(taskInit);
			if (!b){
				return false;
			}
			return true;
		}
		else {
			taskInitMapper.deleteWithCadidateUsers(taskInit);
			Boolean b1 = taskInitMapper.insertWithCadidateUsers(taskInit);
			if (!b1){
				return false;
			}
			return true;
		}
	}

	@Override
	public boolean saveOrUpdateTaskInits(TaskInit taskInit) {
		List<TaskInit> taskInits = taskInitMapper.selectAllByFormKeyAndTaskList(taskInit.getFormKey(), taskInit.getTaskId());
		if (taskInits.isEmpty()){
			Boolean b = taskInitMapper.insertWithCadidateUsers2(taskInit);
			if (!b){
				return false;
			}
			return true;
		}
		else {
			taskInitMapper.deleteWithCadidateUsers(taskInit);
			Boolean b1 = taskInitMapper.insertWithCadidateUsers2(taskInit);
			if (!b1){
				return false;
			}
			return true;
		}
	}


	@Autowired
    private TaskInitMapper taskInitMapper;
}
